<?php
/**
 * Link Management Administration Screen.
 */
/** Load WordPress Administration Bootstrap */
require_once ('admin.php');
global $current_user;
$id = '';
$featured = DealFlight::FEATURED_NO;

if (isset($_GET['i'])) {
    $id = $_GET['i'];
    $dealInfo = DealFlight::get($id);
       
    if ($dealInfo) {
        $aImages = unserialize($dealInfo[0]->image);
	//store session because I want 
	if (!isset($_REQUEST['publish_product'])) {
	    $_SESSION['aImages'] = $aImages;
	    unset($_SESSION['aRemovedImg']);
	}
    } else {
        wp_redirect('404.php');
    }
} else {
    wp_redirect('404.php');
}
$featured=$dealInfo[0]->is_feature;
?>
<?php
if (isset($_REQUEST['deleted'])) {
    echo '<div id="message" class="updated"><p>';
    $deleted = (int) $_REQUEST['deleted'];
    printf(_n('%s link deleted.', '%s links deleted', $deleted), $deleted);
    echo '</p></div>';
    $_SERVER['REQUEST_URI'] = remove_query_arg(array('deleted'), $_SERVER['REQUEST_URI']);
}

$sucessful = 0;
$error = array();
$fileName = '';
$ok = 1;
/*
 * Submit
 */
if (isset($_REQUEST['publish_product'])) {


    if ($_REQUEST['name'] == "") {
        $error['name'] = "Name can not be blank";
        $ok = 0;
    }

     if(isset($_FILES['image'])){
	
	/**
	 * validate image
	 */
	for($i = 0; $i < count($_FILES['image']['name']); $i++){
	    if ($_FILES['image']['name'][$i]) {	
		$name_img_small = $_FILES['image']['name'][$i];
		$ds_name_img_small = explode('.', $name_img_small);
		$n_ds_name_img_small = (int) count($ds_name_img_small) - 1;
		$duopifile = $ds_name_img_small[$n_ds_name_img_small];
		$fileName = time() . '.' . $duopifile;
		
		if (($duopifile == 'jpg') || ($duopifile == "JPG") || ($duopifile == "png") ||
			($duopifile == "PNG") || ($duopifile == "gif") || ($duopifile == "GIF") ||
			($duopifile == "jpeg") || ($duopifile == "JPEG")) {
		  
		} else {
		    $error['image'] = "Please upload a valid image";
		    $ok = 0;break;
		}
	    }
	}
	/**
	 * 
	 */
	if($ok == 1){
	    for($i = 0; $i < count($_FILES['image']['name']); $i++){
		if ($_FILES['image']['name'][$i]) {

		    $name_img_small = $_FILES['image']['name'][$i];
		    $ds_name_img_small = explode('.', $name_img_small);
		    $n_ds_name_img_small = (int) count($ds_name_img_small) - 1;
		    $duopifile = $ds_name_img_small[$n_ds_name_img_small];
		    $fileName = uniqid() . '.' . $duopifile;
		    
		    move_uploaded_file($_FILES['image']['tmp_name'][$i], DEAL_PATH . $fileName);
		    array_push($_SESSION['aImages'], $fileName);		   
		}
	    }
	}
    }    
        
    if ($_REQUEST['description'] == "") {
        $error['description'] = "Description can not be blank";
        $ok = 0;
    }

    if ($_REQUEST['price'] == "") {
        $error['price'] = "Price can not be blank";
        $ok = 0;
    } else {
        if (preg_match('/^[^0-9\.]/', $_REQUEST['price'])) {
            $error['price'] = "Price must be a number";
            $ok = 0;
        }
    }

    if ($_REQUEST['discount'] == "") {
        $error['discount'] = "Discount can not be blank";
        $ok = 0;
    } else {
        if (preg_match('/^[^0-9\.]/', $_REQUEST['discount'])) {
            $error['discount'] = "Discount must be a number";
            $ok = 0;
        }
    }

    
    if($_REQUEST['start_time']=="")
    {
        $error['start_time'] = "Start time can not be blank";
    }
    if($_REQUEST['end_time'] == ""){ 
        
        $error['end_time'] = "End time can not be blank";
                
    }
    
    if ($_REQUEST['start-location'] == "") {
        $error['start_location'] = "Leaving from can not be blank";
        $ok = 0;
    }
    if ($_REQUEST['end_location'] == "") {
        $error['end_location'] = 'Going to can not be blank';
        $ok = 0;
    }
    
    
     if($_REQUEST['categogy']=="")
    {
        $error['categogy'] = "You must choose category";
        $ok = 0 ;
    }
    
    
    if ($ok == 1) {
        $sucessful = 1;
        DealFlight::update($id, $_REQUEST['name'],$_POST['description'], $_SESSION['aImages'], $_REQUEST['price'], 
		$_REQUEST['discount'], $_REQUEST['start_time'], $_REQUEST['end_time'],  DealFlight::FEATURED_NO, 
		$_REQUEST['order'],$_REQUEST['categogy'],0,$_REQUEST['start-location'], $_REQUEST['end_location'], 
		$_REQUEST['adult'], $_REQUEST['children'], $_REQUEST['infant'], $_REQUEST['class'], $_REQUEST['city']);
	//
	if(isset($_SESSION['aRemovedImg'])){
	    foreach($_SESSION['aRemovedImg'] as $imgRemove){
		deleteFile(DEAL_PATH.$imgRemove);		
	    }
	    unset($_SESSION['aRemovedImg']);
	}
	$dealInfo = DealFlight::get($_GET['i']);
	$aImages = unserialize($dealInfo[0]->image);
	
    }
}

?>    

<?php include_once ('./admin-header.php'); ?>
<link rel="stylesheet" type="text/css" href="css/tuan.css"/>

<div class="wrap nosubsub">
    <h2>Update Deal<?php
        if (!empty($_REQUEST['s']))
            printf('<span class="subtitle">' . __('Search results for &#8220;%s&#8221;') . '</span>', esc_html(wp_unslash($_REQUEST['s'])));
        ?>
    </h2>

    <form id="posts-filter" method="post"  enctype="multipart/form-data"> 
        <?php // @$wp_list_table->search_box(__('Search Links'), 'link');  ?>
        <hr>
        <div style="width:100%; background:#FDFDFD">
	    <?php if(isset($sucessful) && $sucessful){
		?>
		<div class="alert alert-success">You have updated successfully!</div>
	    <?php
	    }
	    ?>
            <table width="707"  height="77" cellspacing="0" >
                <tr>
                    <th width="138" valign="top"  scope="row" align="left">
                        <label for="siteupdates">
                            <p class="labelp"><?php _e('Name') ?> (*)</p>  
                        </label>
                    </th>
                    <td width="563"  valign="top">
                        <div style="margin-top:10px;">:  			    
                            <input  style="width:80%; height:30px;" name="name" type="text" id="name"   
                                    value="<?Php echo $dealInfo[0]->name ?>"  class="regular-text"> 
                            <span class="errorclass"><Br/><?php echo $error['name']; ?></span>
                        </div>
                    </td>
                </tr>

                <tr>
                    <th width="138" valign="top"  scope="row" align="left">
                        <label for="siteupdates">
                            <p class="labelp" ><?php _e('Description'); ?> (*)</p> 
                        </label>
                    </th>
                    <td width="563"  valign="top">
                        <?php echo wp_editor($dealInfo[0]->description, 'description', array('textarea_name' => 'description')); ?>
                        <span class="errorclass"><br/><?php echo $error['description']; ?></span>
                    </td>
                </tr>

                <tr>
                    <th width="138" valign="top"  scope="row" align="left">
                        <label for="siteupdates">
                            <p class="labelp" ><?php _e('Image') ?></p> 
                        </label>
                    </th>
                    <td width="563"  valign="top">
                     
			<ul>
			    <?php if(count($aImages) > 0){
				foreach($aImages as $img){
				?>
				<li>
				    <img src="<?php echo TIMTHUMB_URL ?>?src=<?php echo DEAL_URL . $img ?>&w=100&h=100"/>
				    <a href="javascript:void(0)" title="Delete image" image-name="<?php echo $img?>"
				       class="button button-primary button-large delete-image">X</a>
				</li>
			    <?php }
			    }?>
			</ul>
			
			<input type="file" name="image[]"/>
			<br/>
			<a href="javascript:void(0)" class="add-file button button-primary button-large"> + Add file </a>
                        <span class="errorclass"><br/><?php echo $error['image']; ?></span>
                    </td>
                </tr>
                <tr>
                    <th width="138" valign="top"  scope="row" align="left"><label for="siteupdates">
                            <p class="labelp" ><?php _e('Price') ?>
                                (*): </p> </label>
                    </th>
                    <td width="563"  valign="top">
                        <div style="margin-top:10px; margin-left:5px;">
                            <input type='text' name='price' value='<?php echo $dealInfo[0]->price ?>'/>
                            <span class="errorclass"><br/><?php echo $error['price']; ?></span>
                        </div>
                    </td>
                </tr>

                <tr>
                    <th width="138" valign="top"  scope="row" align="left"><label for="siteupdates">
                            <p class="labelp" ><?php _e('Discount') ?>
                            </p> </label>
                    </th>
                    <td width="563"  valign="top">
                        <div style="margin-top:10px; margin-left:5px;">
                            <input type='text' name='discount' value='<?php echo $dealInfo[0]->discount ?>'/>
                            <span class="errorclass"><br/><?php echo $error['discount']; ?></span>
                        </div>
                    </td>
                </tr>

               
                <tr>
                    <th width="138" valign="top"  scope="row" align="left">
                        <label for="siteupdates">
                            <p class="labelp" ><?php _e('Order in list of featured deals') ?></p> 
                        </label>
                    </th>
                    <td width="563"  valign="top">
                        <div style="margin-top:10px; margin-left:5px;">
                            <select name='order'>
                                <option value='1' <?php if ($dealInfo[0]->order == 1) echo 'selected'; ?>>1</option>
                                <option value='2' <?php if ($dealInfo[0]->order == 2) echo 'selected'; ?>>2</option>
                                <option value='3' <?php if ($dealInfo[0]->order == 3) echo 'selected'; ?>>3</option>
                                <option value='4' <?php if ($dealInfo[0]->order == 4) echo 'selected'; ?>>4</option>
                            </select>
                        </div>
                    </td>
                </tr> 
               
                <tr>
                    <th width="138" valign="top"  scope="row" align="left">
                        <label for="siteupdates">
                            <p class="labelp" ><?php _e('Leaving from') ?></p> 
                        </label>
                    </th>
                    <td width="563"  valign="top"  class="td-type-flight">
                        <input value="<?php echo $dealInfo[0]->start_location; ?>" placeholder="Enter City/airport" type="text"
                               id="leaving-from" name="start-location">
                        <span class="errorclass" >
                            <?php echo $error['start_location']; ?>
                        </span>
                    </td>
                </tr>
                
                <tr>
                    <th width="138" valign="top"  scope="row" align="left">
                        <label for="siteupdates">
                            <p class="labelp" ><?php _e('Going to') ?></p> 
                        </label>
                    </th>
                    <td width="563"  valign="top" class="td-type-flight" >
                        <input value="<?php echo $dealInfo[0]->end_location; ?>" placeholder="Enter City/airport" type="text" 
                               id="end_location" name="end_location">
                        <span class="errorclass"><?php echo $error['end_location']; ?></span>
                    </td>
                </tr>
                 <tr>
                    <th width="138" valign="top"  scope="row" align="left" >
                        <label for="siteupdates"> Departure date</label>
                    </th>
                    <td width="563"  valign="top" >
                        <input  value="<?php 
                       
                         $startdate =  $dealInfo[0]->start_time; 
                         $Liststartdate = @explode(" ", $startdate);
                        //echo $Liststartdate;
                         $days = @$Liststartdate[0];
                         $days = explode("-", $days);
                         $day = @$days[2]."/".@$days[1]."/".@$days[0];
                         $house = @$Liststartdate[1];
                         $listHouser = @explode(':', $house);
                         $house = @$listHouser[0].":".@$listHouser[1];
                         echo $day;
                        
                        ?>" id="start_time"    type="text" name="start_time" />
                        <img id="image_bt_start_time" width="20px;" src="<?php echo get_template_directory_uri(); ?>/images/icon_datetime.png"/>
                        <p class="errorclass"><?php echo $error['start_time']; ?></p>
                    </td>
                </tr>
                <tr>
                    <th width="138" valign="top"  scope="row" align="left" >
                        <label for="siteupdates"> Return date</label>
                    </th>
                    <td width="563"  valign="top" >
                        <input  value="<?php  
                            $startdate =  $dealInfo[0]->end_time; 
                         $Liststartdate = @explode(" ", $startdate);
                        //echo $Liststartdate;
                         $days = @$Liststartdate[0];
                         $days = explode("-", $days);
                         $day = @$days[2]."/".@$days[1]."/".@$days[0];
                         $house = @$Liststartdate[1];
                         $listHouser = @explode(':', $house);
                         $house = @$listHouser[0].":".@$listHouser[1];
                         echo $day;
                        
                        
                        ?>" id="end_time"    type="text" name="end_time" />
                        <img id="image_bt_end_time" width="20px;" src="<?php echo get_template_directory_uri(); ?>/images/icon_datetime.png"/>
                        <p class="errorclass"><?php echo $error['end_time']; ?></p>
                    </td>
                </tr>
                 <tr>
                    <th width="138" valign="top"  scope="row" align="left" >
                        <label for="siteupdates">Adult(12+) </label>
                    </th>
                    <td width="563"  valign="top" >
                        <?php
                        $adult = $dealInfo[0]->adult;
                        ?>


                        <select  id="adult" name="adult" class="select-find-flights" >
                            <?php
                            for ($i = 1; $i <= 9; $i++) {
                                if ($adult == $i) {
                                    echo '<option selected=selected value="' . $i . '" >' . $i . '</option>';
                                } else {
                                    echo '<option value="' . $i . '" >' . $i . '</option>';
                                }
                            }
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <th width="138" valign="top"  scope="row" align="left" >
                        <label for="siteupdates">Children(2-12)</label>
                    </th>
                    <td width="563"  valign="top" >
                        <?php $children = $dealInfo[0]->children; ?>
                        <select  id="children" name="children" class="select-find-flights" >
                            <?php
                            for ($i = 0; $i < 9; $i++) {

                                if ($i == $children) {

                                    echo '<option selected=selected  value="' . $i . '" >' . $i . '</option>';
                                } else {
                                    echo '<option value="' . $i . '" >' . $i . '</option>';
                                }
                            }
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <th width="138" valign="top"  scope="row" align="left" >
                        <label for="siteupdates">Infant(0-2)</label>
                    </th>
                    <td width="563"  valign="top" >
                        <?php $infant = $dealInfo[0]->infant; ?>
                        <select id="infant" name="infant"  class="select-find-flights">

                            <option <?php
                            if ($infant == 0) {
                                echo 'selected=selected';
                            }
                            ?> >0</option>
                            <option <?php
                            if ($infant == 1) {
                                echo 'selected=selected';
                            }
                            ?> >1</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <th width="138" valign="top"  scope="row" align="left" >
                        <label for="siteupdates">Class</label>
                    </th>
                    <td width="563"  valign="top" >
                        <?php $class_f = $dealInfo[0]->class; ?>
                        <select name="class" id="class" class="select-find-flights">
                            <option value="<?php echo DealFlight::ECONOMY; ?>"  
                            <?php
                            if ($class_f == DealFlight::ECONOMY) {
                                echo 'selected=selected';
                            }
                            ?>>Economy</option>
                            <option value="<?php echo DealFlight::BUSINESS; ?>" 
                            <?php
                            if ($class_f == DealFlight::BUSINESS) {
                                echo 'selected=selected';
                            }
                            ?> >Business</option>
                            <option value="<?PHP echo DealFlight::FIRST_CLASS ?>" 
                            <?php
                            if ($class_f == DealFlight::FIRST_CLASS) {
                                echo 'selected=selected';
                            }
                            ?> >First Class</option>
                            <option value="<?php echo DealFlight::PREMIUM_ECONOMY ?>
                            <?php
                            if ($class_f == DealFlight::PREMIUM_ECONOMY) {
                                echo 'selected=selected';
                            }
                            ?> ">Premium Economy</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <th width="138" valign="top"  scope="row" align="left" >
                        <label for="siteupdates">City</label>
                    </th>
                    <td width="563"  valign="top" >

                        <select name="city" id="city" class="select-find-flights" >
                            <?php
                            $city = $dealInfo[0]->city;
                            ?>

                            <option  <?php
                            if ($city == 'USA') {
                                echo 'selected="selected"';
                            }
                            ?> value="USA">USA</option>

                            <option  <?php
                            if ($city == 'India') {
                                echo 'selected="selected"';
                            }
                            ?> value="India">India</option>
                            <option value="Dubai"  <?php
                            if ($city == 'Dubai') {
                                echo 'selected="selected"';
                            }
                            ?> > Dubai </option>
                        </select>
                    </td>
                </tr>
                <?php
                /*
                <tr>
                    <th width="138" valign="top"  scope="row" align="left">
                        <label for="siteupdates">
                            <p class="labelp" ><?php _e('Destination Essential') ?></p> 
                        </label>
                    </th>
                    <td width="563"  valign="top">
                        <div style="margin-top:10px; margin-left:5px;">
                            <?php
                            $listCategoriesDeal = Category::getAllCategoryByType(Category::DEAL);

                            $cat_data = $dealInfo[0]->category_id;
                            if ($listCategoriesDeal <> "") {
                                ?>
                                <select name='categogy' id="categogy">
                                    <option value="">Choose Category</option>
                                    <?php
                                    foreach ($listCategoriesDeal as $row) {
                                        $idCat = $row->category_id;
                                        $nameCat = $row->name;
                                        if ($idCat == $cat_data) {
                                            ?>
                                            <option selected="selected" value="<?php echo $idCat ?>">
                                                <?php echo $nameCat; ?>
                                            </option>
                                            <?php
                                        } else {
                                            ?>
                                            <option value="<?php echo $idCat ?>">
                                                <?php echo $nameCat; ?>
                                            </option>
                                            <?php
                                        }
                                    }
                                }
                                ?>

                            </select>
                            <span class="errorclass"><br/><?php echo $error['categogy']; ?></span>
                        </div>
                    </td>
                </tr> 
               */ ?>
                <input  type="hidden" name='categogy' id="categogy" value="0" />
                <tr>
                    <th valign="top"  scope="row" align="left">&nbsp;</th>
                    <td  valign="top"><div style="margin-top:10px; margin-left:5px;">
                            <input type="submit" name="publish_product" id="publish_product" 
                                   class="button button-primary button-large" value="Update" accesskey="p"></div>
                    </td>
                </tr>
            </table>
        </div>
    </form>

</div>

<link rel="stylesheet" type="text/css" href="../wp-content/themes/travel_donald/css/jquery.datetimepicker.css" />
<script src="../wp-content/themes/travel_donald/js/jquery.js"></script>
<script src="../wp-content/themes/travel_donald/js/jquery.datetimepicker.js"></script>

<script type="text/javascript" lang="javascript">
   

    $('#image_bt_start_time').click(function() {
        $('#start_time').datetimepicker('show'); //support hide,show and destroy command
    });


    $('#image_bt_end_time').click(function() {
        $('#end_time').datetimepicker('show'); //support hide,show and destroy command
    });
    
    $(function() {
        $('#start_time').datetimepicker({
            format: 'd/m/Y',
            onShow: function(ct) {
                this.setOptions({
                    maxDate: $('#end_time').val() ? $('#end_time').val() : false
                })
            },
            timepicker: false
        });
        $('#end_time').datetimepicker({
            format: 'd/m/Y',
            onShow: function(ct) {
                this.setOptions({
                    minDate: $('#start_time').val() ? $('#start_time').val() : false
                })
            },
            timepicker: false
        });
    });







</script>

<?php
include('./admin-footer.php');
?>

<script src="<?php bloginfo('template_directory')?>/js/admin/file.js"></script>